#include "gui/GuiInputBox.h"
#include "base/d3d.h"
#include "input/input.h"

#include <dinput.h>

const float GuiInputBox::CursorBlinkSpeed = 150.0f;

GuiInputBox::GuiInputBox(unsigned int textcolor, unsigned int bgcolor, float x, float y, float wide, float tall)  {
   _x = x; _y = y;
   _width = wide; _height = tall;
   _bgcolor = bgcolor;
   _txtcolor = textcolor;
   _curText = "";
   _cursorDelay = 0.0;
}

void GuiInputBox::Draw(Device * device, double tElapsed) const {
   // draw background
   device->setColor(_bgcolor);
   device->drawRect(_x + _width / 2.0f, _y + _height / 2.0f, _width, _height);
   device->setColor(0xFF000000);
   device->drawLine(_x + _width, _y + _height, _x, _y + _height);
   device->drawLine(_x, _y + _height, _x, _y);
   device->setColor(0xFFFFFFFF);
   device->drawLine(_x, _y, _x + _width, _y);
   device->drawLine(_x + _width, _y, _x + _width, _y + _height);

   std::string drawTxt = " > ";
   if (_cursorDelay < 0)  {
      drawTxt += _curText;
   } else  {
      drawTxt += _curText + '|';
   }
   device->drawText(drawTxt.c_str(), 2 /* change */, _x + (device->deviceWidth() / 2.0f) + 1.0f, -_y + (device->deviceHeight() / 2.0f) - 14.0f, _width, _height, 0xffffffff);

}

void GuiInputBox::Update(long mouseX, long mouseY, unsigned long key, double tElapsed)  {
   if (key)  {
      _curText += "key ";
   }

   if (_cursorDelay + tElapsed >= CursorBlinkSpeed)
      _cursorDelay = -1.0;
   else if (_cursorDelay - tElapsed <= -CursorBlinkSpeed)
      _cursorDelay = 1.0;

   _cursorDelay = (_cursorDelay < 0) ? _cursorDelay - tElapsed : _cursorDelay + tElapsed;

}
